var data1 = []
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;

lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   this.getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}

function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,
        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}


let salesmanList=[];
let deptSalesmanList=[];
//select over
(function () {
    //获取当前用户的部门及人员结构树
    let treeList = getCurrentDepartmentUser();
    if(treeList && treeList.list){
        let userTree = getAllJson(treeList.list);
        salesmanList=getxmselect("#salesmanCode","salesmanCode", userTree);
    }else {
        salesmanList=getxmselect("#salesmanCode","salesmanCode", [{name: user.name, value: user.code}]);
    }
    //获取当前用户所属部门树()
    let currentDeptTreeList = getAllUserDepartmentTree();
    if (currentDeptTreeList && currentDeptTreeList.list) {
        let deptTree = getAllJson(currentDeptTreeList.list);
        deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", deptTree);
    } else {
        deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", [{name: user.name, value: user.code}]);
    }
})()

form.render();//表单刷新
var data2=[]
table.render({
    elem: '#foreignexchangesettlementselect'
    ,data:data2
    ,url:'clearChange/selectCostByPage'
    ,toolbar: '#foreignexchangesettlementtoolbar'
    ,totalRow: true
    ,cols: [[
        ,{field:'invoice_no', title:'发票号码',sort:true,width: 120, totalRowText: '合计'}
        ,{field:'contract_no', title:'销售合同号',sort:true,width: 120}
        ,{field:'price_term', title:'价格术语',sort:true,width: 120}
        ,{field:'money_kind', title:'币种',sort:true,width: 120}
        ,{field:'usd_rate', title:'美元汇率',sort:true,width: 120}
        ,{field:'rmb_rate', title:'人民币汇率',sort:true,width: 120}
        ,{field:'cost_money', title:'金额小计',sort:true,width: 120, totalRow: true}
        ,{field:'usd', title:'美元小计',sort:true,width: 120, templet: function(element){
                if(element.money_kind=="RMB"){
                    return (element.cost_money/element.rmb_rate).toFixed(2);
                }else {
                    return (element.cost_money*element.usd_rate).toFixed(2);
                }
            }, totalRow: true
        }
        ,{field:'rmb', title:'人民币小计',sort:true,width: 120, templet: function(element){
            if(element.money_kind=="RMB"){
                return element.cost_money;
            }else {
                return (element.cost_money*element.usd_rate * element.rmb_rate).toFixed(2);
                 }
            }, totalRow: true}
        ,{field:'cost_desc', title:'费用描述',sort:true,width: 120}
        ,{field:'customer_name', title:'客户名',sort:true,width: 120}
        ,{field:'invoice_date', title:'发票日期',sort:true,width: 120}
        ,{field:'salesman', title:'业务员',sort:true,width: 120}
    ]]
    ,id: 'foreignexchangesettlementtableid'
    ,height: $(document).height() - $('#foreignexchangesettlementselect').offset().top - 50
    ,limits:[100,300,500]
    ,limit: 100
    ,page: true
});

form.on('submit(submitform)', function(data){
    table.reload('foreignexchangesettlementtableid', {
        data:data1 ,
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

$(document).ready(function () {
    $.ajax({
        url: 'clearChange/selectAllUser',
        type: 'GET',
        success: function (date) {
            var sm=date.data
            for(let i in sm){
                $("#salesman_code").append("<option value='"+sm[i].code+"'>"+sm[i].name+"</option>")
            }
            form.render();
        }
    });

});

$("#clear").on("click",function () {
    salesmanList.setValue([ ]);
    deptSalesmanList.setValue([ ]);
});